﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;
using Maticsoft.DBUtility;//Please add references
namespace Golf.WebUI
{
    public partial class GolfProducts : System.Web.UI.Page
    {
        /// <summary>
        /// 产品 bll是咧
        /// </summary>
        private Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind_Type();
                Bind_Brand();
                Bind_Top_Selling();//加载最畅销的
            }
        }

        #region 绑定商品类型 数量等  分类购物
        /// <summary>
        /// 绑定商品类型 数量等  分类购物
        /// </summary>
        public void Bind_Type()
        {
            DataSet ds = DbHelperSQL.Query(" select b.Id,b.TypeName,COUNT(a.Id) as pro_count from Golf_Product a right join Golf_ProductType b on a.Pro_TypeId=b.Id where b.IsShow=1 and b.ParentId=1 group by b.Id,b.TypeName order by pro_count desc");
            DataTable dt = ds.Tables[0];
            List<Pro_Str> list = new List<Pro_Str>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Pro_Str p;
                for (int n = 0; n < rowsCount; n++)
                {
                    p = new Pro_Str();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        p.Str1 = dt.Rows[n]["ID"].ToString();
                    }
                    if (dt.Rows[n]["TypeName"] != null && dt.Rows[n]["TypeName"].ToString() != "")
                    {
                        p.Str2 = dt.Rows[n]["TypeName"].ToString();
                    }
                    if (dt.Rows[n]["pro_count"] != null && dt.Rows[n]["pro_count"].ToString() != "")
                    {
                        p.Str3 = dt.Rows[n]["pro_count"].ToString();
                    }
                    list.Add(p);
                }
            }

            rep_Type.DataSource = list;
            rep_Type.DataBind();
            rep_Type_1.DataSource = list;
            rep_Type_1.DataBind();

            Bind_rep_type_pro(list);
        }
        #endregion

        #region 绑定 品牌购物 数量等  分类购物
        /// <summary>
        /// 绑定 品牌购物 数量等  分类购物
        /// </summary>
        public void Bind_Brand()
        {
            DataSet ds = DbHelperSQL.Query("select b.Id,b.TypeName,COUNT(a.Id) as pro_count from Golf_Product a,Golf_BrandType b where a.Pro_BrandTypeId=b.Id group by b.Id,b.TypeName order by pro_count desc");
            DataTable dt = ds.Tables[0];
            List<Pro_Str> list = new List<Pro_Str>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Pro_Str p;
                for (int n = 0; n < rowsCount; n++)
                {
                    p = new Pro_Str();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        p.Str1 = dt.Rows[n]["ID"].ToString();
                    }
                    if (dt.Rows[n]["TypeName"] != null && dt.Rows[n]["TypeName"].ToString() != "")
                    {
                        p.Str2 = dt.Rows[n]["TypeName"].ToString();
                    }
                    if (dt.Rows[n]["pro_count"] != null && dt.Rows[n]["pro_count"].ToString() != "")
                    {
                        p.Str3 = dt.Rows[n]["pro_count"].ToString();
                    }
                    list.Add(p);
                }
            }

            rep_Brand.DataSource = list;
            rep_Brand.DataBind();
        }

        #endregion

        #region  嵌套绑定数据   每个类型绑定最多4个商品 的那个dang
        /// <summary>
        /// 绑定类型
        /// </summary>
        /// <param name="list"></param>
        private void Bind_rep_type_pro(List<Pro_Str> list)
        {
            this.rep_type_pro.DataSource = list;
            this.rep_type_pro.DataBind();
        }

        protected void rep_type_pro_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate，

            //HeaderTemplate，，ItemTemplate，SeparatorTemplate）
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                Label pan = e.Item.FindControl("_Null") as Label;//找到里层的repeater对象
                Repeater rep = e.Item.FindControl("rep_pro") as Repeater;//找到里层的repeater对象
                Pro_Str rowv = (Pro_Str)e.Item.DataItem;//找到分类Repeater关联的数据项 
                int typeid = Convert.ToInt32(rowv.Str1); //获取填充子类的id 
                List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(4, " Pro_TypeId=" + typeid, " id").Tables[0]);
                if (list.Count == 0)
                {
                    pan.Visible = true;
                }
                else
                {

                    pan.Visible = false;
                }
                foreach (var item in list)
                {
                    string[] temp = item.Pro_Picture.Split(',');
                    item.Pro_Picture = temp[0];
                    //处理 节省值
                    item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
                }
                rep.DataSource = list;
                rep.DataBind();
                Script_Write(list);
            }
        }
        #endregion

        #region   这里的四个方法 感觉和首页的类似            数据待添加

        #region 最畅销的  数据待添加
        /// <summary>
        /// 最畅销的
        /// </summary>
        public void Bind_Top_Selling()
        {
            DataSet ds = Maticsoft.DBUtility.DbHelperSQL.Query("select b.ProductId,COUNT(*) aa from dbo.Golf_OrderProduct b where 1=1 group by b.ProductId order by aa desc");
            DataTable dt = ds.Tables[0];
            StringBuilder sb = new StringBuilder();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                for (int n = 0; n < rowsCount; n++)
                {
                    if (dt.Rows[n]["ProductId"] != null && dt.Rows[n]["ProductId"].ToString() != "")
                    {
                        sb.Append(dt.Rows[n]["ProductId"].ToString() + ",");
                    }
                }
            }
            string sb1 = "";
            if (sb.Length != 0)
            {
                sb1 = sb.ToString().Substring(0, sb.Length - 1);
            }
            else
            {
                return;
            }
            List<Golf.Model.Golf_Product> list = bll.GetModelList(" id in(" + sb1 + ")");
            foreach (var item in list)
            {
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_udpp_pro.DataSource = list;
            rep_udpp_pro.DataBind();
            Script_Write(list);
        }

        #endregion

        #region 最高额定    数据待添加


        #endregion

        #region 最近添加
        public void Bind_link_Added()
        {
            // order by  Pro_AddTime desc 按时间降序排列
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(18, "", " Pro_AddTime desc").Tables[0]);
            foreach (var item in list)
            {
                ////只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_udpp_pro.DataSource = list;
            rep_udpp_pro.DataBind();
            Script_Write(list);
        }

        #endregion

        #region 特别优惠
        /// <summary>
        ///特别优惠   使用 促销商品
        /// </summary>
        public void Bind_OffersSelling()
        {
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(4, "IsPromotion=1", " Pro_PromotionPrice").Tables[0]);
            foreach (var item in list)
            {
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_udpp_pro.DataSource = list;
            rep_udpp_pro.DataBind();
            Script_Write(list);
        }

        #endregion

        #endregion
        protected void tabs_Click(object sender, EventArgs e)
        {
            link_Selling.CssClass = "";
            link_Rated.CssClass = "";
            link_Added.CssClass = "";
            link_OffersSelling.CssClass = "";
            LinkButton link = (LinkButton)sender;
            link.CssClass = "open";
            switch (link.ID)
            {
                case "link_Selling"://最畅销的
                    Bind_Top_Selling();
                    break;
                case "link_Rated"://最高额定
                    Bind_Top_Selling();
                    break;
                case "link_Added"://最近添加
                    Bind_link_Added();
                    break;
                case "link_OffersSelling"://特别优惠
                    Bind_OffersSelling();
                    break;
            }
            udppCarousel.Update();//当点击Button(aaaa)时就在后台刷新MYID中的html内容了 

        }
        #region   生成页面脚本    鼠标移动到上面的
        /// <summary>
        /// 生成页面脚本    鼠标移动到上面的
        /// </summary>
        public void Script_Write(List<Golf.Model.Golf_Product> list)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.Append(" <script language=\"JavaScript\">  \n");
            foreach (var item in list)
            {
                //ID，名称，名优产品，HREF，图片，CommercialMsg，价格，规格，保存，评价，视频，缺货，折扣
                //ID, Name, BrandName, Href, Image, CommercialMsg, Price, RRP, Save, Rating, Video, OOS, Discount
                sbScript.Append("MAD.products.addProduct(new MAD.entities.product(\"" + item.Id + "\", \" " + item.Pro_Name + "\", \"" + new Index().GetBrandName((int)item.Pro_BrandTypeId) + "\", \"ProductDetail.aspx?id=" + item.Id + "\", \"ImageProducts/" + item.Pro_PicBig.Split(',')[0] + "\", \"" + item.Pro_Name + "\", \"" + item.Pro_PromotionPrice + "\", \"" + item.Pro_MarketPrice + "\", \"" + item.Pro_Save + "\", \"" + item.Pro_Star + "\", \"1\")); \n");
            }
            sbScript.Append(" </script> \n");
            script_pro.InnerHtml += sbScript.ToString();
        }
        #endregion
    }
}
